package com.qysoft.modules.genTable.service;

import com.qysoft.modules.genTable.entity.*;
import com.qysoft.modules.genTable.util.GenUtils;
import com.qysoft.rapid.dao.mybatis.common.CommonDao;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GenSchemeService {


    public static void createCode(GenScheme scheme) throws Exception {

        StringBuilder result = new StringBuilder();
        // 查询主表及字段列
        Map map = new HashMap();
        map.put("tableName",scheme.getGen_table_id());
        GenTable genTable = CommonDao.selectOneByXml("query_genTable_one",map);
        genTable.setColumnList(CommonDao.<GenTableColumn>selectListByXml("findColumnList",genTable));
        // 获取所有代码模板
        GenConfig config = GenUtils.getConfig();
        // 获取模板列表
        List<GenTemplate> templateList = GenUtils.getTemplateList(config, scheme.getCategory(), false);
        //List<GenTemplate> childTableTemplateList = GenUtils.getTemplateList(config, genScheme.getCategory(), true);
        // 生成主表模板代码
        scheme.setGen_table_id(genTable.getId());
        Map<String, Object> model = GenUtils.getDataModel(scheme,genTable);
        for (GenTemplate tpl : templateList){
            result.append(GenUtils.generateToFile(tpl, model, true));
        }
        System.out.println(templateList);
    }
}
